"use strict";

var Endpoint    =   function(sHost, iPort) {
    this.sProtocol  =   "";
    this.sHost      =   sHost ? sHost : "";
    this.iPort      =   iPort ? iPort : 0;
    this.iTimeout   =   0;
}

Endpoint.prototype.toString =   function() {
    return (this.sProtocol + " -h " + this.sHost + " -p " + this.iPort + " -t " + this.iTimeout).replace(/^[\s\t ]+|[\s\t ]+$/g, "");
}

Endpoint.prototype.copy =   function() {
    var temp    =   new Endpoint();
    temp.sProtocol  =   this.sProtocol;
    temp.sHost      =   this.sHost;
    temp.iPort      =   this.iPort;
    temp.iTimeout   =   this.iTimeout;

    return temp;
}

Endpoint.parse  =   function(desc) {
    var items   =   desc.split(" ");
    var protocol =  items[0].toLowerCase();
    if(protocol != "http" && protocol != "tcp" && protocol != "udp") {
        return undefined;
    }

    var endpoint = new Endpoint();
    endpoint.sProtocol  =   protocol;

    for(var i = 1; i < items.length; i++) {
        if(items[i] === "-h") {
            endpoint.sHost  = items[++i];
            continue;
        }

        if(items[i] === "-p") {
            endpoint.iPort  =   parseInt(items[++i]);
            continue;
        }

        if(items[i] === "-t") {
            endpoint.iTimeout   =  parseInt(items[++i]);
            continue;
        }
    }

    return endpoint;
}

module.exports  =   Endpoint;